﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;
using System.Web.Profile;

using Gift.DAL;
using Gift.Model;

namespace WebSite.cn
{
    public partial class register_modify : BasePage
    {
        readonly DALMember theMember = new DALMember();
        tbMember oMember;
        // Fields
  

        // Methods
        private void Bind()
        {
            oMember = theMember.GetModel(int.Parse(base.LoginUserMember.MemberId));
          
            if (oMember != null)
            {
                this.txtUserName.Value = oMember.memberlogin;
                this.hdUserName.Value = oMember.memberlogin;
                this.txtName.Value = oMember.membername;
                //this.tmpSex = oMember.sex;
                this.txtMobile.Value = oMember.phone;

                this.txtTel.Value = oMember.tel1;
               // this.tmpPost = oMember.post;
                this.txtAddress.Value = oMember.address;
                //this.tmpMsn = oMember.msn;
                //this.tmpQQ = oMember.qq;
                this.txtEmail.Value = oMember.email;
                this.hdEmail.Value = oMember.email;
                this.hdId.Value = oMember.memberid.ToString();
                

                //this.username.Value = tmpUsername;
                //this.cname.Value = tmpCname;
                //this.email.Value = tmpEmail;
                //if (this.tmpSex == "0" || this.tmpSex == "1")
                //{
                //    this.rbtSex.Items.FindByValue(this.tmpSex).Selected = true;
                //}
            }
        }

        protected void btnInfoUpdate_Click(object sender, EventArgs e)
        {
            string str3;
            DataTable table;
            string strUserName = base.LoginUserMember.MemberName;
            string str4 = "";
            if (this.txtUserName.Value != base.LoginUserMember.MemberName)
            {
                table = SQLAccess.GreatDs(string.Concat(new object[] { "select memberid from tbMember where memberlogin='", base.LoginUserMember.MemberName, "' and memberid<>", base.LoginUserMember.MemberId, "" })).Tables[0];
                if (table.Rows.Count > 0)
                {
                    this.ClientScript.RegisterClientScriptBlock(this.Page.ClientScript.GetType()
                                                         , "msg"
                                                         , "<script language=javascript>alert('用户名已存在');window.history.go(-1);</script>");

                    return;
                }
                else
                {
                    strUserName = base.Request.Form["uname"];
                }
            }
            if (!string.IsNullOrEmpty(this.txtOldPwd.Value))
            {
                table = SQLAccess.GreatDs(string.Concat(new object[] { "select memberid from tbMember where memberpwd='", FormsAuthentication.HashPasswordForStoringInConfigFile(re.replace(this.txtOldPwd.Value), "MD5"), "' and memberid=", base.LoginUserMember.MemberId, "" })).Tables[0];
                if (table.Rows.Count > 0)
                {
                    str4 = FormsAuthentication.HashPasswordForStoringInConfigFile(re.replace(this.txtUserPwd.Value), "MD5");
                }
                else
                {
                    this.ClientScript.RegisterClientScriptBlock(this.Page.ClientScript.GetType()
                                                            , "msg"
                                                            , "<script language=javascript>alert('原始密码输入有误');</script>");

                    return;
                }
            }

            oMember = theMember.GetModel(int.Parse(base.LoginUserMember.MemberId));
            if (oMember != null)
            {
                oMember.memberlogin = strUserName;
                oMember.memberpwd = str4 != "" ? str4 : oMember.memberpwd;
                oMember.membername = this.txtName.Value.Trim();
                //oMember.sex = this.rbtSex.SelectedItem.Value;
                oMember.phone = this.txtMobile.Value.Trim();// base.Request.Form["tel"];
                oMember.tel1 = this.txtTel.Value.Trim();// base.Request.Form["phone"];
                oMember.email = this.txtEmail.Value.Trim();// this.email.Value.Trim();
                //oMember.msn = base.Request.Form["msn"];
                //oMember.qq = base.Request.Form["qq"];
                oMember.address = this.txtAddress.Value.Trim();// base.Request.Form["address"];
                //oMember.post = base.Request.Form["post"];
                theMember.Update(oMember);

                this.ClientScript.RegisterClientScriptBlock(this.Page.ClientScript.GetType(), "msg", "<script language=javascript>alert('更新成功');window.location.href='register_modify.aspx';</script>");
            }
         
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                if (!string.IsNullOrEmpty(base.LoginUserMember.MemberId))
                {
                    this.Bind();
                }
                else
                {
                    this.ClientScript.RegisterClientScriptBlock(this.Page.ClientScript.GetType(), "msg", "<script language=javascript>window.location.href='index.aspx';</script>");
                }
            }
        }





    }
}